<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleSyn({})" v-auth="'rd:rd32:rdrecord32:edit'">
          <Icon icon="i-simple-line-icons:refresh" /> {{ t('同步数据') }}
        </a-button>
        <a-button type="default" v-auth="'rd:rd32:rdrecord32:edit'" @click="handleRevoke({})">
          <Icon icon="ant-design:reload-outlined" /> {{ t('整单撤销') }}
        </a-button>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="success" ghost @click="handleForm({})" v-auth="'rd:rd32:rdrecord32:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id, sourceFlag: record.sourceFlag, handSte: record.handSte })">
          {{ record.fbillno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <InputFormView @register="registerViewDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <listModal @register="registerListModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsRdRd32Rdrecord32List">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    rdrecord32Delete,
    rdrecord32ListData,
    rdrecord32Syn,
    rdrecord32Revoke,
  } from '/@/api/wms/rd/rd32/rdrecord32';
  import { companyTreeData } from '/@/api/sys/company';
  import { useGlobSetting } from '/@/hooks/setting';
  import { Popconfirm } from 'ant-design-vue';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputFormView from './formView.vue';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { useUserStore } from '/@/store/modules/user';
  import listModal from './listModal.vue';
  const userStore = useUserStore();

  const { t } = useI18n('rd.rd32.rdrecord32');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('出库任务单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司'),
        field: 'companyCode',
        fieldLabel: 'companyName',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('发货单号'),
        field: 'fbillno',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'createDate',
        // component: 'DatePicker',
        component: 'RangePicker',
      },
      {
        label: t('发货日期'),
        field: 'fdate',
        component: 'RangePicker',
      },
      {
        label: t('发运方式'),
        field: 'fyfs',
        component: 'Input',
      },
      {
        label: t('购货单位名称'),
        field: 'fsuppliername',
        component: 'Input',
      },
      {
        label: t('客户联系人'),
        field: 'fcontacts',
        component: 'Input',
      },
      {
        label: t('客户电话'),
        field: 'fphone',
        component: 'Input',
      },
      {
        label: t('到站'),
        field: 'fdz',
        component: 'Input',
      },
      {
        label: t('省份'),
        field: 'fprovince',
        component: 'Input',
      },
      {
        label: t('城市'),
        field: 'fcity',
        component: 'Input',
      },
      {
        label: t('是否打印'),
        field: 'fifprinteger',
        component: 'Input',
      },
      {
        label: t('紧急程度'),
        field: 'furgentlevel',
        component: 'Select',
        componentProps: {
          dictType: 'wms_jj_level',
          allowClear: true,
        },
      },
      {
        label: t('部门'),
        field: 'fdepartment',
        component: 'Input',
      },
      {
        label: t('业务员'),
        field: 'femp',
        component: 'Input',
      },
      
      // {
      //   label: t('是否撤销'),
      //   field: 'status',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'rd32_cx_status',
      //     allowClear: true,
      //     onChange: handleSuccess,
      //   },
      // },
    ],
    fieldMapToTime: [['fdate', ['fdate_gte', 'fdate_lte']],['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('发货单号'),
      dataIndex: 'fbillno',
      key: 'a.fbillno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('紧急程度'),
      dataIndex: 'furgentlevel',
      key: 'a.furgentlevel',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'wms_jj_level',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('发货日期'),
      dataIndex: 'fdate',
      key: 'a.fdate',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('发运方式'),
      dataIndex: 'fyfs',
      key: 'a.fyfs',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('购货单位内码'),
      dataIndex: 'fsupplyid',
      key: 'a.fsupplyid',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('购货单位编码'),
      dataIndex: 'fsupplierno',
      key: 'a.fsupplierno',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('购货单位名称'),
      dataIndex: 'fsuppliername',
      key: 'a.fsuppliername',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('客户联系人'),
      dataIndex: 'fcontacts',
      key: 'a.fcontacts',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('客户电话'),
      dataIndex: 'fphone',
      key: 'a.fphone',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('到站'),
      dataIndex: 'fdz',
      key: 'a.fdz',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('省份'),
      dataIndex: 'fprovince',
      key: 'a.fprovince',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('城市'),
      dataIndex: 'fcity',
      key: 'a.fcity',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('是否打印'),
      dataIndex: 'fifprinteger',
      key: 'a.fifprinteger',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('部门'),
      dataIndex: 'fdepartment',
      key: 'a.fdepartment',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('业务员'),
      dataIndex: 'femp',
      key: 'a.femp',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('主管'),
      dataIndex: 'fempz',
      key: 'a.fempz',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('制单'),
      dataIndex: 'fbiller',
      key: 'a.fbiller',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('是否打包'),
      dataIndex: 'fifpack',
      key: 'a.fifpack',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('公司编码'),
      dataIndex: 'companyCode',
      key: 'a.company_code',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('公司名称'),
      dataIndex: 'companyName',
      key: 'a.company_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    // {
    //   title: t('是否撤销'),
    //   dataIndex: 'status',
    //   key: 'a.status',
    //   dictType: 'rd32_cx_status',
    //   sorter: true,
    //   width: 100,
    //   align: 'center',
    //   fixed: 'left',
    // },
    {
      title: t('备注'),
      dataIndex: 'fexplanation',
      key: 'a.fexplanation',
      sorter: true,
      width: 230,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 80,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑出库任务单'),
        onClick: handleForm.bind(this, { id: record.id, sourceFlag: record.sourceFlag }),
        auth: 'rd:rd32:rdrecord32:edit',
        // handSte 为 1 时不显示
        ifShow: () => {
          return record.sourceFlag !== '0' && record.handSte !== '1';
        },
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除出库任务单'),
        popConfirm: {
          title: t('是否确认删除出库任务单'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'rd:rd32:rdrecord32:edit',
        ifShow: () => {
          return record.sourceFlag !== '0' && record.handSte !== '1';
        },
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerViewDrawer, { openDrawer: openViewDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys, setProps }] = useTable({
    api: rdrecord32ListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    console.log(record, 'record');
    if (record.sourceFlag === '0' || record.handSte === '1') {
      openViewDrawer(true, record);
    } else {
      openDrawer(true, record);
    }
    // openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await rdrecord32Delete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      // url: ctxAdminPath + '/rd/rd32/rdrecord32/exportData',
      url: ctxAdminPath + '/rd/rd32/rdrecord32/exportMainData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  // 同步K3出库任务单
  async function handleSyn() {
    const res = await rdrecord32Syn({});
    showMessage(res.message);
    handleSuccess({});
  }

  // 整单撤销
  const [registerListModal, { openModal: openListModal }] = useModal();
  async function handleRevoke() {
    if (getSelectRowKeys().length == 1) {
      // const res = await rdrecord32Revoke({ id: getSelectRowKeys()[0] });
      // showMessage(res.message);
      // handleSuccess({});
      openListModal(true, { id: getSelectRowKeys()[0] });
    } else {
      showMessage('请先选择一条数据！');
    }
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  onMounted(async () => {
    setProps()
    handleCompany();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
    });
  }
</script>
